package com.qdaily.ui.util;

import android.content.Context;
import android.content.SharedPreferences;
import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.controller.ActivityController;

/**
 * Created by song on 15/5/14.
 * 进这个页面肯定是debug模式
 */
public class QDTestServerSetHelper {
    private static QDTestServerSetHelper ourInstance = new QDTestServerSetHelper();

    private Context mContext;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private final String KeyIsUseTestServer = "IsUseTest";
    private final String KeyTestServer = "TestServerIP";
    private static String serverIP;
    private static boolean isUseTestServer = false;

    public static QDTestServerSetHelper getInstance() {
        return ourInstance;
    }

    private QDTestServerSetHelper() {
    }

    public void init(Context context) {
        mContext = context;
        sp = context.getApplicationContext().getSharedPreferences("ServerConfig", 0);
        isUseTestServer = sp.getBoolean(KeyIsUseTestServer, true);  //只要进这个界面，默认是测试版
        serverIP = sp.getString(KeyTestServer, "http://app.qdaily.com/"); //只要进这个界面，默认是测试版
    }

    public Boolean isUseTestServer() {
        return isUseTestServer;
    }

    public void setTestServer(Boolean useTest, String ip) {
        isUseTestServer = useTest;
        serverIP = ip;
        editor = sp.edit();
        editor.clear();
        editor.putBoolean(KeyIsUseTestServer, useTest);
        editor.putString(KeyTestServer, ip);
        if (editor.commit()) {
            ActivityController.getInstance().exit();
        }
    }

    public static String getServerIp() {
        if (isUseTestServer) {
            QLog.d("Debug ServerIp", serverIP);
            return serverIP;
        }
        return "http://app.qdaily.com/";
    }
}
